#include "CEventManager.h"

CEventManager::CEventManager()
{

}
CEventManager::~CEventManager()
{
	ClearCallback();
}
HRESULT CEventManager::ClearCallback()
{
	for (std::map<const std::string, PyObject*>::iterator iter = m_pEventHandler.begin(); iter != m_pEventHandler.end(); iter++)
	{
		Py_XDECREF(iter->second);
		iter->second = NULL;
	}
	m_pEventHandler.clear();
	return S_OK;
}

PyObject* CEventManager::GetCallback(const std::string& evtName)
{
	std::map<const std::string, PyObject*>::iterator iter = m_pEventHandler.find(evtName);
	if (iter == m_pEventHandler.end())
	{
		return NULL;
	}
	return iter->second;
}

PyObject* CEventManager::SetCallback(const std::string& eventName, PyObject* pCallback)
{
	if (!PyCallable_Check(pCallback))
	{
		PyErr_SetString(PyExc_TypeError, "pCallback is not callable!!");
		return NULL;
	}
	std::map<const std::string, PyObject*>::iterator iter = m_pEventHandler.find(eventName);
	if (iter != m_pEventHandler.end())
	{
		Py_XDECREF(iter->second);
		iter->second = NULL;
	}
	m_pEventHandler[eventName] = pCallback;
	Py_XINCREF(pCallback);
	return PyLong_FromLong(0);
}

